package com.pzdHome.action;

import java.math.BigDecimal;
import java.util.List;
import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import com.pzdHome.dao.model.Puser;
import com.pzdHome.service.PuserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.pzdHome.dao.model.UserRecharge;
import com.pzdHome.dao.model.UserRechargeExample;
import com.pzdHome.service.UserRechargeService;

/**
@author sun
*/
@RequestMapping("userRecharge")
@Controller
@LoginFilter
public class UserRechargeController extends CoreController{

    @Autowired
    private UserRechargeService userRechargeService;
    @Autowired
	private PuserService puserService;

    @RequestMapping("/list")
	@ResponseBody
    public String list(Integer page, Integer rows){
    	PaginationSupport.byPage(page, rows);
    	UserRechargeExample example = new UserRechargeExample();

    	List<UserRecharge> list = userRechargeService.selectByExample(example);
      	return page(list);
    }

    @RequestMapping("/save")
	@ResponseBody
    public String save(Integer id, String money, String giftMoney){
		//查找用户
		Puser puser = puserService.selectByPrimaryKey(id);

		UserRecharge c = new UserRecharge();
		c.setAddMoney(money);
		c.setGiftMoney(giftMoney);
		c.setUserId(puser.getId());
		c.setUserAccount(puser.getAccount());
		userRechargeService.insert(c);

		//总计金额
		BigDecimal totoalMoney = new BigDecimal(money).add(new BigDecimal(giftMoney));

		puser.setMoney(new BigDecimal(puser.getMoney()).add(totoalMoney).toString());
		puserService.updateByPrimaryKeySelective(puser);
       	return ok();
    }

    @RequestMapping("/findById")
	@ResponseBody
    public String find(Integer id){
    	UserRecharge userRecharge = userRechargeService.selectByPrimaryKey(id);
       	return ok(userRecharge);
    }

    @RequestMapping("/del")
	@ResponseBody
    public String del(String id){
    	String[] ids = id.split(",");
    	for (String str : ids){
    		userRechargeService.deleteByPrimaryKey(Integer.parseInt(str));
    	}
       	return ok();
    }


}